package com.ruoyi.web.controller.dy;

import java.util.Arrays;
import java.util.Date;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.TlDyGoodsVo;
import com.ruoyi.system.service.ITlDyGoodsVoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TlDyGoods;
import com.ruoyi.system.service.ITlDyGoodsService;
import com.ruoyi.common.core.page.TableDataInfo;

import static com.ruoyi.common.core.page.TableSupport.buildPageRequest;

/**
 * 抖音商品Controller
 *
 * @author ruoyi
 * @date 2023-10-19
 */
@RestController
@RequestMapping("/system/dygoods")
public class TlDyGoodsController extends BaseController
{
    @Autowired
    private ITlDyGoodsService tlDyGoodsService;
    @Autowired
    private ITlDyGoodsVoService iTlDyGoodsVoService;

    /**
     * 查询抖音商品列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TlDyGoodsVo tlDyGoodsVo)
    {
        tlDyGoodsVo.setId(null);
        //先判断有没有没有结束采集的记录
        SysUser user = SecurityUtils.getLoginUser().getUser();
        TlDyGoodsVo t = iTlDyGoodsVoService.selectByUserIdAndState(user.getUserId(), 0L);
        if(t!=null){
            throw new ServiceException("存在未结束采集任务,请先结束采集！");
        }
        //        startPage();
        PageDomain pageDomain =  buildPageRequest();
        TableDataInfo list = tlDyGoodsService.selectTlDyGoodsList(tlDyGoodsVo,pageDomain);
        if(list.getTotal()==0){
            return list;
        }
        tlDyGoodsVo.setCreateTime(new Date());
        tlDyGoodsVo.setUserId(user.getUserId());
        tlDyGoodsVo.setState(0L);
        //将List转Stirng
        if(tlDyGoodsVo.getCategoryList()!=null && tlDyGoodsVo.getCategoryList().size()>0){
            String CategoryListString = tlDyGoodsVo.getCategoryList().stream()
                    .map(String::valueOf)
                    .collect(Collectors.joining("#"));
            tlDyGoodsVo.setCategoryListString(CategoryListString);
        }
        if(tlDyGoodsVo.getSensitiveWords()!=null && tlDyGoodsVo.getSensitiveWords().size()>0){
            String SensitiveWords = String.join("#", tlDyGoodsVo.getSensitiveWords());
            tlDyGoodsVo.setSensitiveWordsString(SensitiveWords);
        }
        if(tlDyGoodsVo.getTitlesWords()!=null && tlDyGoodsVo.getTitlesWords().size()>0){
            String TitlesWords = String.join("#", tlDyGoodsVo.getTitlesWords());
            tlDyGoodsVo.setTitlesWordsString(TitlesWords);
        }
        iTlDyGoodsVoService.insertTlDyGoodsVo(tlDyGoodsVo);
        return list;
    }

    /**
     * 停止采集
     * @param
     * @return
     */
    @GetMapping("/stopList")
    public TableDataInfo stopList()
    {
        TableDataInfo list = tlDyGoodsService.stopList();
        return list;
    }

    /**
     * 查询采集状态
     * @return
     */
    @GetMapping("/findState")
    public AjaxResult findState()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        TlDyGoodsVo tlDyGoodsVo = iTlDyGoodsVoService.selectByUserIdAndState(user.getUserId(), 0L);
        return AjaxResult.success(tlDyGoodsVo);
    }

    /**
     * 导出抖音商品列表
     */
    @Log(title = "抖音商品", businessType = BusinessType.EXPORT)
    @PostMapping( "/export" )
    public void export(HttpServletResponse response,@RequestParam("ids") String ids)
    {
        tlDyGoodsService.export(response, Arrays.asList(ids.split(",")),null);
//        ExcelUtil<TlDyGoods> util = new ExcelUtil<TlDyGoods>(TlDyGoods.class);
//        util.exportExcel(response,list, "抖音商品数据");
    }

    /**
     * 获取抖音商品详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tlDyGoodsService.selectTlDyGoodsById(id));
    }

    /**
     * 新增抖音商品
     */
    @Log(title = "抖音商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TlDyGoods tlDyGoods)
    {
        return toAjax(tlDyGoodsService.insertTlDyGoods(tlDyGoods));
    }

    /**
     * 修改抖音商品
     */
    @Log(title = "抖音商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TlDyGoods tlDyGoods)
    {
        return toAjax(tlDyGoodsService.updateTlDyGoods(tlDyGoods));
    }

    /**
     * 删除抖音商品
     */
    @Log(title = "抖音商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tlDyGoodsService.deleteTlDyGoodsByIds(ids));
    }
    //抖音商品数据同步
    @Log(title = "抖音商品数据同步", businessType = BusinessType.UPDATE)
    @PostMapping("/dataSync")
    public AjaxResult dataSync(){
        return toAjax(tlDyGoodsService.dataSync());
    }

    /**
     *
     * @return
     */
    @PostMapping("/saveES")
    public AjaxResult saveES(){
        int i = tlDyGoodsService.saveES();
        return toAjax(i);
    }
}
